package com.lijinjiang.listener;

import com.lijinjiang.component.CustomListCellRender;
import com.lijinjiang.component.JDatePanel;
import com.lijinjiang.constant.Colors;
import com.lijinjiang.constant.Times;
import com.lijinjiang.mode.PickerMode;
import com.lijinjiang.model.DateModel;
import com.lijinjiang.util.PopupUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * @Description 点击时间标签监听
 * @Author lijinjiang
 * @Date 2024-12-30 10:52
 */
public class ClickTimeLabelListener implements MouseListener {
    private final JDatePanel datePanel;
    private final DateModel<?> dateModel;
    private final PickerMode pickerMode;

    public ClickTimeLabelListener(JDatePanel datePanel) {
        this.datePanel = datePanel;
        this.dateModel = datePanel.getDateModel();
        this.pickerMode = datePanel.getPickerMode();
    }

    @Override public void mouseClicked(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON1) {//左键点击
            Object source = e.getSource();
            JPopupMenu popupMenu = new JPopupMenu();
            popupMenu.setLayout(new GridLayout(1, 1));
            JList<String> timeList;
            if (source == datePanel.getHourLabel()) {//点击小时标签
                timeList = new JList<>(Times.HOURS);
                timeList.setSelectedValue(String.format("%02d", dateModel.getHour()), false);
            } else if (source == datePanel.getMinuteLabel()) {//点击分钟标签
                if (pickerMode == PickerMode.MINUTE_10) {
                    timeList = new JList<>(Times.MINUTES_10);
                } else if (pickerMode == PickerMode.MINUTE_15) {
                    timeList = new JList<>(Times.MINUTES_15);
                } else if (pickerMode == PickerMode.MINUTE_30) {
                    timeList = new JList<>(Times.MINUTES_30);
                } else {
                    timeList = new JList<>(Times.MINUTES);
                }
                timeList.setSelectedValue(String.format("%02d", dateModel.getMinute()), false);
            } else if (source == datePanel.getSecondLabel()) {//点击秒标签
                timeList = new JList<>(Times.SECONDS);
                timeList.setSelectedValue(String.format("%02d", dateModel.getSecond()), false);
            } else {
                timeList = new JList<>();
            }
            timeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//设置单选
            timeList.setCellRenderer(new CustomListCellRender<>());
            timeList.addListSelectionListener(l -> {
                if (source == datePanel.getHourLabel()) {
                    dateModel.setHour(Integer.parseInt(String.valueOf(timeList.getSelectedValue()).trim()));
                    ((JLabel) source).setText(String.format("%02d", dateModel.getHour()) + " 时");//回写到小时标签上
                } else if (source == datePanel.getMinuteLabel()) {
                    dateModel.setMinute(Integer.parseInt(String.valueOf(timeList.getSelectedValue()).trim()));
                    ((JLabel) source).setText(String.format("%02d", dateModel.getMinute()) + " 分");//回写到分钟标签上
                } else if (source == datePanel.getSecondLabel()) {
                    dateModel.setSecond(Integer.parseInt(String.valueOf(timeList.getSelectedValue()).trim()));
                    ((JLabel) source).setText(String.format("%02d", dateModel.getSecond()) + " 秒");//回写到秒标签上
                }
                popupMenu.setVisible(false); //隐藏菜单
            });
            JScrollPane timeScrollPane = new JScrollPane(timeList);
            JScrollBar verticalScrollBar = timeScrollPane.getVerticalScrollBar();
            // 设置滚动条大小
            verticalScrollBar.setPreferredSize(new Dimension(3, 0));
            int index = timeList.getSelectedIndex();//被选中的索引
            timeScrollPane.getViewport().setViewPosition(new Point(0, 20 * index));//设置滚动条的显示位置
            timeScrollPane.setBorder(null);
            popupMenu.add(timeScrollPane);
            PopupUtil.showPopupMenu(((JLabel) source), popupMenu, timeList.getModel().getSize());
        }
    }

    @Override public void mousePressed(MouseEvent e) {

    }

    @Override public void mouseReleased(MouseEvent e) {

    }

    @Override public void mouseEntered(MouseEvent e) {
        Object source = e.getSource();
        if (source instanceof JLabel) {
            JLabel label = (JLabel) source;
            label.setForeground(Colors.FG_ENTERED);
        }

    }

    @Override public void mouseExited(MouseEvent e) {
        Object source = e.getSource();
        if (source instanceof JLabel) {
            JLabel label = (JLabel) source;
            label.setForeground(Color.BLACK);
        }
    }
}
